package demo.dcs.util;

import demo.dcs.TraceGenerater2;
import demo.java.lang.management.jvm.AppTraceConfig;
import demo.java.lang.management.vo.XmlyLogSpan;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by dbwind.liu on 17/7/25.
 */
public class ErrorlogSpanUtil {
    private static final Logger logger = LoggerFactory.getLogger(ErrorlogSpanUtil.class);

    //eventType:用来区分不同业务的错误
    //logIdentity:用来区分具体的错误,报警的标识
    //logDetail用来记录错误的具体信息,自己设置
    public static void append( String eventType, String logIdentity, String logDetail) {
        append(null, null, eventType, logIdentity, logDetail);
    }
    public static void append(Throwable throwable) {
        try {
            XmlyLogSpan span = new XmlyLogSpan();
            span.setLevel("ERROR");
            span.setTraceId(TraceGenerater2.getTracer().getTraceIdThreadLocal());
            span.setSpanId(TraceGenerater2.getTracer().getSpanIdThreadLocal());
            span.setMonitorId(TraceGenerater2.getTracer().getMonitorIdThreadLocal());
            String serviceId=null;
            if(StringUtils.isBlank(serviceId)){
                serviceId =TraceGenerater2.getTracer().getServiceIdThreadLocal();
                if (StringUtils.isBlank(serviceId)){
                    serviceId = getServiceIdFromInterceptor();
                }
                span.setServiceId(serviceId);
            }else{
                span.setServiceId(serviceId);
            }

            span.setXmlyId(TraceGenerater2.getTracer().getXmlyIdThreadLocal());
            span.setTimestamp(System.currentTimeMillis());
            String appName=null;
            if(StringUtils.isBlank(appName)){
                span.setApp(AppTraceConfig.getLocalConfig().getAppName());
            }else{
                span.setApp(appName);
            }
            span.setHostName(AppTraceConfig.getLocalConfig().getHostName());
            span.setIp(AppTraceConfig.getLocalConfig().getIp());
            span.setLogDetail(throwable.getMessage());
            span.setEventType(throwable.getClass().getName());
            span.setProcessId(AppTraceConfig.getLocalConfig().getProcessId());
            span.setLogIdentity(throwable.getMessage());

            TraceGenerater2.getTracer().writeLogSpan(span);
        } catch (Throwable ex) {
            logger.error("handle append error", ex);
        }
    }

    public static void append(String appName, String serviceId, String eventType, String logIdentity, String logDetail) {

        try {
            XmlyLogSpan span = new XmlyLogSpan();
            span.setLevel("ERROR");
            span.setTraceId(TraceGenerater2.getTracer().getTraceIdThreadLocal());
            span.setSpanId(TraceGenerater2.getTracer().getSpanIdThreadLocal());
            span.setMonitorId(TraceGenerater2.getTracer().getMonitorIdThreadLocal());
            if(StringUtils.isBlank(serviceId)){
                serviceId =TraceGenerater2.getTracer().getServiceIdThreadLocal();
                if (StringUtils.isBlank(serviceId)){
                    serviceId = getServiceIdFromInterceptor();
                }
                span.setServiceId(serviceId);
            }else{
                span.setServiceId(serviceId);
            }

            span.setXmlyId(TraceGenerater2.getTracer().getXmlyIdThreadLocal());
            span.setTimestamp(System.currentTimeMillis());
            if(StringUtils.isBlank(appName)){
                span.setApp(AppTraceConfig.getLocalConfig().getAppName());
            }else{
                span.setApp(appName);
            }
            span.setHostName(AppTraceConfig.getLocalConfig().getHostName());
            span.setIp(AppTraceConfig.getLocalConfig().getIp());
            span.setLogDetail(logDetail);
            span.setEventType(eventType);
            span.setProcessId(AppTraceConfig.getLocalConfig().getProcessId());
            span.setLogIdentity(logIdentity);

            TraceGenerater2.getTracer().writeLogSpan(span);
        } catch (Throwable ex) {
            logger.error("handle append error", ex);
        }
    }

    public static void appendInfo( String eventType, String logIdentity, String logDetail) {
        appendInfo(null, null, eventType, logIdentity, logDetail);
    }

    public static void appendInfo(String appName, String serviceId, String eventType, String logIdentity, String logDetail) {

        try {
            XmlyLogSpan span = new XmlyLogSpan();
            span.setLevel("INFO");
            span.setTraceId(TraceGenerater2.getTracer().getTraceIdThreadLocal());
            span.setSpanId(TraceGenerater2.getTracer().getSpanIdThreadLocal());
            span.setMonitorId(TraceGenerater2.getTracer().getMonitorIdThreadLocal());
            if(StringUtils.isBlank(serviceId)){
                serviceId =TraceGenerater2.getTracer().getServiceIdThreadLocal();
                if (StringUtils.isBlank(serviceId)){
                    serviceId = getServiceIdFromInterceptor();
                }
                span.setServiceId(serviceId);
            }else{
                span.setServiceId(serviceId);
            }

            span.setXmlyId(TraceGenerater2.getTracer().getXmlyIdThreadLocal());
            span.setTimestamp(System.currentTimeMillis());
            if(StringUtils.isBlank(appName)){
                span.setApp(AppTraceConfig.getLocalConfig().getAppName());
            }else{
                span.setApp(appName);
            }
            span.setHostName(AppTraceConfig.getLocalConfig().getHostName());
            span.setIp(AppTraceConfig.getLocalConfig().getIp());
            span.setLogDetail(logDetail);
            span.setEventType(eventType);
            span.setProcessId(AppTraceConfig.getLocalConfig().getProcessId());
            span.setLogIdentity(logIdentity);

            TraceGenerater2.getTracer().writeLogSpan(span);
        } catch (Throwable ex) {
            logger.error("handle append error", ex);
        }
    }

    private static String getServiceIdFromInterceptor() {
        //走spring mvc interceptor
        if (SpringTraceContext.getSpringTraceContext()!=null && SpringTraceContext.getSpringTraceContext().getSpringTraceModel()!=null) {
            return SpringTraceContext.getSpringTraceContext().getSpringTraceModel().getRestUrl();
        }
        return null;
    }
}
